

import light.im.ActionDispather;
import light.im.IMEndpoint;
import light.im.loginserver.action.EchoAction;
import light.im.loginserver.action.LoginAction;
import light.network.ByteData;
import light.network.Packet;
import light.network.UdpPacket;
import light.network.PackProtocal;
import light.network.object.ActionPack;


public class RunServer {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		IMEndpoint s = new IMEndpoint("localhost", 10606);
		ActionDispather dis = new ActionDispather(s);
		dis.registerAction(new LoginAction());
		dis.registerAction(new EchoAction());
		s.setDispatcher(dis);
		s.start();
		
		runclient();
	}
	public static void runclient() throws Exception
	{
		ActionPack loginInfo = new ActionPack("echo");
		loginInfo.put("id", "psw");
		
		Packet bf = UdpPacket.allocate(1020);
		loginInfo.setData(new ByteData("xxxx".getBytes(), 0));
		PackProtocal.unwrap(loginInfo, bf);
		
//		UdpEndpoint end = new UdpEndpoint();
//		end.startListener();
//		end.send(bf, new InetSocketAddress("localhost", 10606));
//		end.setReceiveHandler(new IAIOHandler<Object>() {
//			@Override
//			public void completed(UdpNetworkSession<Object> session) {
//				Pack d = session.getDataBuffer();
//				ByteBuffer pp = d.buffer();
//				System.out.println(new String(pp.array(), 0, pp.limit()));
//			}
//			@Override
//			public void failed(Throwable exc, UdpNetworkSession<Object> session) {
//			}
//		});
		
		IMEndpoint s = new IMEndpoint("localhost", 10106);
		ActionDispather dis = new ActionDispather(s);
		dis.registerAction(new LoginAction());
		dis.registerAction(new EchoAction());
		s.setDispatcher(dis);
		s.start();
		//s.send(bf, new InetSocketAddress("localhost", 10606));
	}

}
